package com.hongguan.oms.web.meeting;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.service.meeting.MeetingInfoService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;
/**
 * @author <a href="mailto:jiangting@hongguaninfo.com">jiangting</a>
 * @version createTime:2010-1-25 ����11:11:11 
 * summary
 */
public class MeetingHandler extends BaseHandler{
	private MeetingInfoService service;
	public void setService(MeetingInfoService service) { 
		this.service = service;
	}
	//search meeting info
	public ModelAndView searchMeeting(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView  mv = this.getSuccessModelView(); 
		Map<String,Object> param=new HashMap<String,Object>();
		if(Util.toNull(request.getParameter("mtCreator"))!=null&&Util.toNull(request.getParameter("mtCreator"))!=""){
			param.put("mt_creator", "%"+Util.toNull(request.getParameter("mtCreator").trim().toLowerCase())+"%");
		}		
		param.put("mt_end_date",Util.toNull(request.getParameter("mtEndDate")));
		param.put("mt_start_date", Util.toNull(request.getParameter("mtStartDate")));
		if(Util.toNull(request.getParameter("mtName"))!=null&&Util.toNull(request.getParameter("mtName"))!=""){
			param.put("mt_name", "%"+Util.toNull(request.getParameter("mtName").trim().toLowerCase())+"%");
		}		
		mv.addObject("result",service.searchMeeting(param));
		return mv;
	}
	/**
	 * search meeeting info by meeting id
	 * @param request
	 * @param respnose
	 * @return
	 * @throws Exception
	 */
	public ModelAndView searchMeetingById(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception{
		ModelAndView mv = this.getSuccessModelView();
		Map<String,Object> param=new HashMap<String,Object>();
	 	param.put("mt_id", Util.toNull(request.getParameter("mtId")));
	 	mv.addObject("result", service.searchMeetingById(param));
		return mv;
	}
	/**
	 * search meeting by datetime
	 * @param request
	 * @param respnose
	 * @return
	 * @throws Exception
	 */
	public ModelAndView searchMeetingByDate(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception{
		ModelAndView mv = this.getSuccessModelView();
	 	Map<String,Object> param=new HashMap<String,Object>();
	 	String mt_start_date= Util.toNull(request.getParameter("mtStartDate"));
	 	param.put("mt_start_date", mt_start_date+" 00:00:00");
	 	param.put("mt_end_date", mt_start_date+" 23:59:59");
	 	param.put("mt_joiner", Util.getUserIdFromSession(request));
	 	mv.addObject("result", service.searchMeetingByDate(param)); 
		return mv;
	}
	/**
	 * add an meeting 
	 * @param request
	 * @param respnose
	 * @return
	 * @throws Exception
	 */
	public ModelAndView addMeeting(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception{
 		ModelAndView mv = this.getSuccessModelView();
		String msg ="";
		try {
			if (this.getSessionUserId(request)) {
				mv = this.getErrorMessageView("无法获得用户名,请重新登录!");
			} else {
				Map<String, Object> param=this.getMeetingInfo(request);
				long flag=service.searchMeetingDateValidate(param);
				//flag<0说明没有与之冲突的会议,反之...
				if(flag==0){
					param.put("mt_creator", Util.getUserIdFromSession(request)); 
					param.put("mt_status", 0); 
					service.addMeeting(param,request);	
				}if(flag>0){
					msg="会议时间冲突";		
					mv.addObject("msg", msg);
				}				
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return mv;
	}
	/**
	 * edit a meeting
	 */
	public ModelAndView editMeeting(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception{
		ModelAndView mv = null;
		String msg ="";
		if (this.getSessionUserId(request)) {
			mv = this.getErrorMessageView("无法获得用户名,请重新登录!");
		} else {
			Map<String, Object> param= new HashMap<String, Object>();
			param.put("mt_start_date", Util.toNull(request.getParameter("mtStartDate")));
			param.put("mt_end_date", Util.toNull(request.getParameter("mtEndDate")));
			long flag=service.searchMeetingDateValidate(param);
			//flag<0说明没有与之冲突的会议,反之...
			if(flag==0){
				service.editMeeting(this.getMeetingInfo(request));
				service.editMeetingJoin(param);
			}if(flag>0){
				msg="会议时间冲突";		
				mv.addObject("msg", msg);
			}	
			mv = this.getSuccessModelView();
		}
		return mv;
	}
	/**
	 * get meeting object
	 */
	private Map<String, Object> getMeetingInfo(HttpServletRequest request) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("mt_id", Util.toNull(request.getParameter("mtId")));
		param.put("mt_name", Util.toNull(request.getParameter("mtName")));
		param.put("mt_room", Util.toNull(request.getParameter("mtRoom")));
		param.put("mt_start_date", Util.toNull(request.getParameter("mtStartDate")));
		param.put("mt_end_date", Util.toNull(request.getParameter("mtEndDate")));
		param.put("mt_assigner", Util.toNull(request.getParameter("mtAssigner")));
		param.put("mt_joiner", Util.toNull(request.getParameter("mtJoiner")));
		param.put("mt_summary", Util.toNull(request.getParameter("mtSummary")));
		param.put("mt_summary_url", Util.toNull(request.getParameter("mtSummaryUrl")));
		param.put("mt_level", Util.toNull(request.getParameter("mtLevel")));
		param.put("mt_attention", request.getParameter("mtAttention"));
		param.put("mt_notice", request.getParameter("mtNotice"));
		param.put("mt_status", request.getParameter("mtStatus"));
		param.put("user_id", request.getParameter("userId"));
		return param;
	}
	private boolean getSessionUserId(HttpServletRequest request) {
		if (Util.getUserIdFromSession(request) == null)
			return true;
		else
			return false;
	}
}
